package leetcode.code0517;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution517 implements IDebug {

	public int findMinMoves(int[] machines) {
		int total = Arrays.stream(machines).sum();
		int size = machines.length;
		if (total % size != 0)
			return -1;
		int avg = total / size;
		int num = 0;
		int sum = 0;
		int ans = 0;
		for (int i = 0; i < size; i++) {
			num = machines[i] - avg;
			sum += num;
			ans = Math.max(ans, Math.max(Math.abs(sum), num));
		}
		return ans;
	}

	@Override
	public void debug1() {
		String str = "[1,0,5]";
		int[] machines = HelpDebug.str2array3(str);
		int e = 3;
		int r = this.findMinMoves(machines);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		String str = "[0,3,0]";
		int[] machines = HelpDebug.str2array3(str);
		int e = 2;
		int r = this.findMinMoves(machines);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug3() {
		String str = "[0,2,0]";
		int[] machines = HelpDebug.str2array3(str);
		int e = -1;
		int r = this.findMinMoves(machines);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution517 so = new Solution517();
		so.debug1();
		so.debug2();
		so.debug3();

	}

}
